﻿#region imports

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;

using CondeNet.AssetManager.Data;
using CondeNet.AssetManager.ProviderServices;
using CondeNet.AssetManager.ProviderServices.Data;
using CondeNet.AssetManager.Business.Interface;
using CondeNet.AssetManager.DataProvider.Sql.Mappers;

using CondeNet.AssetManager.Business;

#endregion

namespace CondeNet.AssetManager.DataProvider.Sql
{
    public class SearchByMetaData
    {
        private AssetManagerDBDataContext context;

        public IList<IAsset> SearchAssetByKeyValueCombo ( IList<KeyValuePair<string , string>> keyValues , bool keyvaluejoined )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<IAsset> assets = new List<IAsset> ( );

                foreach ( KeyValuePair<string , string> keyval in keyValues )
                {
                    // see if we have a key and value
                    var key = from item in context.T_MetaKeys where keyval.Key.ToUpper ( ).Equals ( item.Name_NVC.ToUpper ( ) ) select item;
                    var val = from item in context.T_MetaValues where keyval.Value.ToUpper ( ).Equals ( item.Value_NVC.ToUpper ( ) ) select item;

                    if ( keyvaluejoined ) // we're looking for content with key&&value
                    {
                        if ( key != null && val != null )
                        {
                            var amd = from item in context.T_AssetMetaDatas where item.MetaKeyID_FK == ( ( T_MetaKey ) key ).MetaKey_IDPK && item.MetaValueID_FK == ( ( T_MetaValue ) val ).MetaValue_IDPK select item;
                            if ( amd != null ) assets.Add ( AssetMapper.ToBusinessObject ( ( T_Asset ) context.GetAssetByID ( ( ( T_AssetMetaData ) amd ).AssetID_FK ) ) );
                        }
                    }
                    else // we're looking for content with key||value
                    {
                        bool assetAdded = false; // we don't want to add the asset twice
                        if ( key != null )
                        {
                            var amd = from item in context.T_AssetMetaDatas where item.MetaKeyID_FK == ( ( T_MetaKey ) key ).MetaKey_IDPK select item;
                            if ( amd != null )
                            {
                                assets.Add ( AssetMapper.ToBusinessObject ( ( T_Asset ) context.GetAssetByID ( ( ( T_AssetMetaData ) amd ).AssetID_FK ) ) );
                                assetAdded = true;
                            }
                        }
                        if ( val != null && !assetAdded )
                        {
                            var amd = from item in context.T_AssetMetaDatas where item.MetaValueID_FK == ( ( T_MetaValue ) val ).MetaValue_IDPK select item;
                            if ( amd != null ) assets.Add ( AssetMapper.ToBusinessObject ( ( T_Asset ) context.GetAssetByID ( ( ( T_AssetMetaData ) amd ).AssetID_FK ) ) );
                        }
                    }
                }
                return assets;
            }
        }

        public IList<IAsset> SearchAssetByKeyValueCombo ( IList<KeyValuePair<string , string>> keyValues , bool keyvaluejoined , DateTime createdLower , DateTime createdUpper , DateTime updatedLower , DateTime updatedUpper )
        {
            using ( context = DataContextFactory.CreateContext ( ) )
            {
                List<IAsset> assets = new List<IAsset> ( );

                foreach ( KeyValuePair<string , string> keyval in keyValues )
                {
                    var key = from item in context.T_MetaKeys where keyval.Key.ToUpper ( ).Equals ( item.Name_NVC.ToUpper ( ) ) select item;
                    var val = from item in context.T_MetaValues where keyval.Value.ToUpper ( ).Equals ( item.Value_NVC.ToUpper ( ) ) select item;

                    if ( keyvaluejoined ) // we're looking for content with key&&value
                    {
                        if ( key != null && val != null )
                        {
                            var amd = from item in context.T_AssetMetaDatas where item.MetaKeyID_FK == ( ( T_MetaKey ) key ).MetaKey_IDPK && item.MetaValueID_FK == ( ( T_MetaValue ) val ).MetaValue_IDPK select item;
                            if ( amd != null )
                            {
                                if ( ( ( ( T_AssetMetaData ) amd ).CreatedDate_DT <= createdUpper && ( ( T_AssetMetaData ) amd ).CreatedDate_DT >= createdLower ) &&
                                    ( ( ( T_AssetMetaData ) amd ).UpdatedDate_DT <= updatedUpper && ( ( T_AssetMetaData ) amd ).UpdatedDate_DT >= updatedLower ) )
                                    assets.Add ( AssetMapper.ToBusinessObject ( ( T_Asset ) context.GetAssetByID ( ( ( T_AssetMetaData ) amd ).AssetID_FK ) ) );
                            }
                        }
                    }
                    else // we're looking for content with key||value
                    {
                        bool assetAdded = false; // we don't want to add the asset twice
                        if ( key != null )
                        {
                            var amd = from item in context.T_AssetMetaDatas where item.MetaKeyID_FK == ( ( T_MetaKey ) key ).MetaKey_IDPK select item;
                            if ( amd != null )
                            {
                                if ( ( ( ( T_AssetMetaData ) amd ).CreatedDate_DT <= createdUpper && ( ( T_AssetMetaData ) amd ).CreatedDate_DT >= createdLower ) &&
                                    ( ( ( T_AssetMetaData ) amd ).UpdatedDate_DT <= updatedUpper && ( ( T_AssetMetaData ) amd ).UpdatedDate_DT >= updatedLower ) )
                                {
                                    assets.Add ( AssetMapper.ToBusinessObject ( ( T_Asset ) context.GetAssetByID ( ( ( T_AssetMetaData ) amd ).AssetID_FK ) ) );
                                    assetAdded = true;
                                }
                            }
                        }
                        if ( val != null && !assetAdded )
                        {
                            var amd = from item in context.T_AssetMetaDatas where item.MetaValueID_FK == ( ( T_MetaValue ) val ).MetaValue_IDPK select item;
                            if ( amd != null )
                            {
                                if ( ( ( ( T_AssetMetaData ) amd ).CreatedDate_DT <= createdUpper && ( ( T_AssetMetaData ) amd ).CreatedDate_DT >= createdLower ) &&
                                    ( ( ( T_AssetMetaData ) amd ).UpdatedDate_DT <= updatedUpper && ( ( T_AssetMetaData ) amd ).UpdatedDate_DT >= updatedLower ) )
                                {
                                    assets.Add ( AssetMapper.ToBusinessObject ( ( T_Asset ) context.GetAssetByID ( ( ( T_AssetMetaData ) amd ).AssetID_FK ) ) );
                                }
                            }
                        }
                    }
                }
                return assets;
            }
        }

        public IList<IAssetContent> SearchAssetContentByKeyValueCombo ( IList<KeyValuePair<string , string>> keyValues , bool keyvaluejoined )
        {
            List<IAssetContent> assetContents = new List<IAssetContent> ( );

            foreach ( KeyValuePair<string , string> keyval in keyValues )
            {
                // see if we have a key and value
                var key = from item in context.T_MetaKeys where keyval.Key.ToUpper ( ).Equals ( item.Name_NVC.ToUpper ( ) ) select item;
                var val = from item in context.T_MetaValues where keyval.Value.ToUpper ( ).Equals ( item.Value_NVC.ToUpper ( ) ) select item;

                if ( keyvaluejoined ) // we're looking for content with key&&value
                {
                    if ( key != null && val != null )
                    {
                        var acmd = from item in context.T_AssetContentMetaDatas where item.MetaKeyID_FK == ( ( T_MetaKey ) key ).MetaKey_IDPK && item.MetaValueID_FK == ( ( T_MetaValue ) val ).MetaValue_IDPK select item;
                        if ( acmd != null ) assetContents.Add ( AssetContentMapper.ToBusinessObject ( ( T_AssetContent ) context.GetAssetContentByID ( ( ( T_AssetContentMetaData ) acmd ).AssetContentID_FK ) ) );
                    }
                }
                else // we're looking for content with key||value
                {
                    bool assetContentAdded = false; // we don't want to add the asset twice
                    if ( key != null )
                    {
                        var acmd = from item in context.T_AssetContentMetaDatas where item.MetaKeyID_FK == ( ( T_MetaKey ) key ).MetaKey_IDPK select item;
                        if ( acmd != null )
                        {
                            assetContents.Add ( AssetContentMapper.ToBusinessObject ( ( T_AssetContent ) context.GetAssetContentByID ( ( ( T_AssetContentMetaData ) acmd ).AssetContentID_FK ) ) );
                            assetContentAdded = true;
                        }
                    }
                    if ( val != null && !assetContentAdded )
                    {
                        var acmd = from item in context.T_AssetMetaDatas where item.MetaValueID_FK == ( ( T_MetaValue ) val ).MetaValue_IDPK select item;
                        if ( acmd != null ) assetContents.Add ( AssetContentMapper.ToBusinessObject ( ( T_AssetContent ) context.GetAssetByID ( ( ( T_AssetContentMetaData ) acmd ).AssetContentID_FK ) ) );
                    }
                }
            }
            return assetContents;
        }

        public IList<IAssetContent> SearchAssetContentByKeyValueCombo ( IList<KeyValuePair<string , string>> keyValues , bool keyvaluejoined , DateTime createdLower , DateTime createdUpper , DateTime updatedLower , DateTime updatedUpper )
        {
            List<IAssetContent> assetContents = new List<IAssetContent> ( );

            foreach ( KeyValuePair<string , string> keyval in keyValues )
            {
                // see if we have a key and value
                var key = from item in context.T_MetaKeys where keyval.Key.ToUpper ( ).Equals ( item.Name_NVC.ToUpper ( ) ) select item;
                var val = from item in context.T_MetaValues where keyval.Value.ToUpper ( ).Equals ( item.Value_NVC.ToUpper ( ) ) select item;

                if ( keyvaluejoined ) // we're looking for content with key&&value
                {
                    if ( key != null && val != null )
                    {
                        var acmd = from item in context.T_AssetContentMetaDatas where item.MetaKeyID_FK == ( ( T_MetaKey ) key ).MetaKey_IDPK && item.MetaValueID_FK == ( ( T_MetaValue ) val ).MetaValue_IDPK select item;
                        if ( acmd != null )
                        {
                            if ( ( ( ( T_AssetContentMetaData ) acmd ).CreatedDate_DT <= createdUpper && ( ( T_AssetContentMetaData ) acmd ).CreatedDate_DT >= createdLower ) &&
                                    ( ( ( T_AssetContentMetaData ) acmd ).UpdatedDate_DT <= updatedUpper && ( ( T_AssetContentMetaData ) acmd ).UpdatedDate_DT >= updatedLower ) )
                            {
                                assetContents.Add ( AssetContentMapper.ToBusinessObject ( ( T_AssetContent ) context.GetAssetContentByID ( ( ( T_AssetContentMetaData ) acmd ).AssetContentID_FK ) ) );
                            }
                        }
                    }
                }
                else // we're looking for content with key||value
                {
                    bool assetContentAdded = false; // we don't want to add the asset twice
                    if ( key != null )
                    {
                        var acmd = from item in context.T_AssetContentMetaDatas where item.MetaKeyID_FK == ( ( T_MetaKey ) key ).MetaKey_IDPK select item;
                        if ( acmd != null )
                        {
                            if ( ( ( ( T_AssetContentMetaData ) acmd ).CreatedDate_DT <= createdUpper && ( ( T_AssetContentMetaData ) acmd ).CreatedDate_DT >= createdLower ) &&
                                       ( ( ( T_AssetContentMetaData ) acmd ).UpdatedDate_DT <= updatedUpper && ( ( T_AssetContentMetaData ) acmd ).UpdatedDate_DT >= updatedLower ) )
                            {
                                assetContents.Add ( AssetContentMapper.ToBusinessObject ( ( T_AssetContent ) context.GetAssetContentByID ( ( ( T_AssetContentMetaData ) acmd ).AssetContentID_FK ) ) );
                                assetContentAdded = true;
                            }
                        }
                    }
                    if ( val != null && !assetContentAdded )
                    {
                        var acmd = from item in context.T_AssetMetaDatas where item.MetaValueID_FK == ( ( T_MetaValue ) val ).MetaValue_IDPK select item;
                        if ( acmd != null )
                        {
                            if ( ( ( ( T_AssetContentMetaData ) acmd ).CreatedDate_DT <= createdUpper && ( ( T_AssetContentMetaData ) acmd ).CreatedDate_DT >= createdLower ) &&
                                    ( ( ( T_AssetContentMetaData ) acmd ).UpdatedDate_DT <= updatedUpper && ( ( T_AssetContentMetaData ) acmd ).UpdatedDate_DT >= updatedLower ) )
                            {
                                assetContents.Add ( AssetContentMapper.ToBusinessObject ( ( T_AssetContent ) context.GetAssetByID ( ( ( T_AssetContentMetaData ) acmd ).AssetContentID_FK ) ) );
                            }
                        }
                    }
                }
            }
            return assetContents;
        }
    }
}
